package com.rn.gypsophila.dsalg.arrary;

import java.util.Arrays;

/**
 * 题目：
 * 编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。
 * <p>
 * 链接：<a href="https://leetcode.cn/leetbook/read/array-and-string/ciekh/">...</a>
 *
 * @author 然诺
 * @since 2023/2/26
 */
public class SetZero {

    public static void setZeroes(int[][] matrix) {
        if (matrix == null) {
            return;
        }
        int rowLen = matrix.length;
        int colLen = matrix[0].length;
        int[] xArray = new int[rowLen];
        int[] yArray = new int[colLen];

        for (int i = 0; i < rowLen; i++) {
            for (int j = 0; j < colLen; j++) {
                if (matrix[i][j] == 0) {
                    xArray[i] = 1;
                    yArray[j] = 1;
                }
            }
        }
        for (int i = 0; i < rowLen; i++) {
            for (int j = 0; j < colLen; j++) {
                if (xArray[i] == 1 || yArray[j] == 1) {
                    matrix[i][j] = 0;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{
                {1, 1, 1},
                {1, 0, 1},
                {1, 1, 1}};
        System.out.println(Arrays.deepToString(matrix));
        //setZeroes(matrix);
        System.out.println(Arrays.deepToString(matrix));

        int[][] matrix2 = new int[][]{
                {0, 1, 2, 0},
                {3, 4, 5, 2},
                {1, 3, 1, 5}};
        System.out.println(Arrays.deepToString(matrix2));
        setZeroes(matrix2);
        System.out.println(Arrays.deepToString(matrix2));
    }

}
